﻿namespace Huayou.UI.WebControls
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    [DefaultProperty("Text"), DefaultEvent("Click"), ToolboxBitmap(typeof(Huayou.UI.WebControls.FileUpload), "Huayou.UI.WebControls.FileUpload.bmp"), ToolboxData("<{0}:FileUpload runat=server></{0}:FileUpload>")]
    public class FileUpload : WebControl
    {
        private Button button = new Button();
        private int[] contentLength;
        private string[] contentType;
        private HtmlInputFile[] file;
        private string[] filename;
        private int filenum = 1;
        private string filter = "";
        private string[] oldfilename;
        private string path = Path.GetTempPath();
        private int size = 0;
        private int successNum = 0;
        private string[] suffix;

        [Description("上传后激发的事件"), Bindable(true), Category("事件")]
        public event EventHandler Click;

        public FileUpload()
        {
            this.FileNum = 1;
            this.button.Click += new EventHandler(this.button_Click);
            this.button.CssClass = this.CssClass;
            this.Controls.Add(this.button);
        }

        private void button_Click(object sender, EventArgs e)
        {
            this.Upload();
            if (this.Click != null)
            {
                this.Click(sender, e);
            }
        }

        public void DeleteFile()
        {
            foreach (string str in this.filename)
            {
                if ((str != null) && !"".Equals(str))
                {
                    File.Delete(this.path + @"\" + str);
                }
            }
        }

        private string GetSuffix(string filename)
        {
            int startIndex = filename.LastIndexOf(".");
            if (startIndex > 0)
            {
                return filename.Substring(startIndex);
            }
            return "";
        }

        private void Upload()
        {
            for (int i = 0; i < this.filenum; i++)
            {
                try
                {
                    HttpPostedFile postedFile = this.file[i].PostedFile;
                    if (postedFile != null)
                    {
                        if ((postedFile.ContentLength > this.size) && (this.size != 0))
                        {
                            break;
                        }
                        string fileName = postedFile.FileName;
                        int num2 = fileName.LastIndexOf(@"\");
                        if (num2 == -1)
                        {
                            break;
                        }
                        this.oldfilename[i] = fileName.Substring(num2 + 1);
                        this.ContentType[i] = postedFile.ContentType;
                        this.ContentLength[i] = postedFile.ContentLength;
                        string suffix = this.GetSuffix(this.oldfilename[i]);
                        if ((this.filter.Length > 0) && (this.filter.IndexOf(suffix) > -1))
                        {
                            break;
                        }
                        this.filename[i] = DateTime.Now.Ticks.ToString();
                        this.suffix[i] = suffix;
                        if (!Directory.Exists(this.path))
                        {
                            Directory.CreateDirectory(this.path);
                        }
                        postedFile.SaveAs(Path.Combine(this.path, this.filename[i] + suffix));
                        this.successNum++;
                    }
                }
                catch
                {
                }
            }
        }

        public override string AccessKey
        {
            get
            {
                return this.button.AccessKey;
            }
            set
            {
                this.button.AccessKey = value;
            }
        }

        public override Color BackColor
        {
            get
            {
                return this.button.BackColor;
            }
            set
            {
                this.button.BackColor = value;
            }
        }

        public override Color BorderColor
        {
            get
            {
                return this.button.BorderColor;
            }
            set
            {
                this.button.BorderColor = value;
            }
        }

        public override System.Web.UI.WebControls.BorderStyle BorderStyle
        {
            get
            {
                return this.button.BorderStyle;
            }
            set
            {
                this.button.BorderStyle = value;
            }
        }

        public int[] ContentLength
        {
            get
            {
                return this.contentLength;
            }
        }

        public string[] ContentType
        {
            get
            {
                return this.contentType;
            }
        }

        public string[] Filename
        {
            get
            {
                return this.filename;
            }
        }

        [Bindable(true), Description("设定上传文件的个数"), Category("参数"), DefaultValue("1")]
        public int FileNum
        {
            get
            {
                return this.filenum;
            }
            set
            {
                if (value < 1)
                {
                    value = 1;
                }
                this.filenum = value;
                this.Controls.Clear();
                this.file = new HtmlInputFile[this.filenum];
                this.filename = new string[this.filenum];
                this.oldfilename = new string[this.filenum];
                this.contentType = new string[this.filenum];
                this.contentLength = new int[this.filenum];
                this.suffix = new string[this.filenum];
                for (int i = 0; i < this.filenum; i++)
                {
                    this.file[i] = new HtmlInputFile();
                    this.file[i].Attributes["class"] = this.CssClass;
                    this.file[i].Style["width"] = (this.Width.Value - this.button.Width.Value) + "";
                    this.Controls.Add(this.file[i]);
                }
                this.button.CssClass = this.CssClass;
                this.file[0].ID = this.file[0].Name;
                this.button.Attributes["onclick"] = "if(" + this.file[0].ID + ".value==''){ alert('请选择上传的文件');return false;}return confirm('请确认上传');";
                this.Controls.Add(this.button);
            }
        }

        public int FileSize
        {
            get
            {
                return this.size;
            }
            set
            {
                this.size = value;
            }
        }

        public string Filter
        {
            get
            {
                return this.filter;
            }
            set
            {
                this.filter = value;
            }
        }

        public string[] OldFilename
        {
            get
            {
                return this.oldfilename;
            }
        }

        public int SuccessNum
        {
            get
            {
                return this.successNum;
            }
        }

        public string[] Suffix
        {
            get
            {
                return this.suffix;
            }
        }

        [Category("Appearance"), Bindable(true), DefaultValue("")]
        public string Text
        {
            get
            {
                return this.button.Text;
            }
            set
            {
                this.button.Text = value;
            }
        }

        [DefaultValue("1"), Description("设定上传文件的路径"), Bindable(true), Category("参数")]
        public string UploadPath
        {
            get
            {
                return this.path;
            }
            set
            {
                if ("".Equals(value) || (value == null))
                {
                    value = Path.GetTempPath();
                }
                this.path = value;
            }
        }

        public int UploadSize
        {
            get
            {
                return this.file[0].Size;
            }
            set
            {
                for (int i = 0; i < this.file.Length; i++)
                {
                    this.file[i].Size = value;
                }
            }
        }
    }
}

